package com.csxy.nyf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.nyf.entity.EduChapter;
import com.csxy.nyf.entity.EduVideo;
import com.csxy.nyf.entity.vo.ChapterVo;
import com.csxy.nyf.entity.vo.VideoVo;
import com.csxy.nyf.exception.MyException;
import com.csxy.nyf.mapper.EduChapterMapper;
import com.csxy.nyf.service.EduChapterService;
import com.csxy.nyf.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //最终要的数据列表
        ArrayList<ChapterVo> finalChapterVos = new ArrayList<>();
        //先根据课程Id查询所有章节
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        List<EduChapter> chapterList = baseMapper.selectList(queryWrapper);
        //在根据课程Id查询所有小节
        QueryWrapper<EduVideo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("course_id",courseId);
        List<EduVideo> videoList = eduVideoService.list(queryWrapper1);

        //拼接章节和小节chapterList[{EduChapter1,class},{EduChapter,class}]
        for (EduChapter chapter : chapterList) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            finalChapterVos.add(chapterVo);

            List<VideoVo> finalVideoList = new ArrayList<>();
            for (EduVideo video : videoList) {
                if(chapter.getId().equals(video.getChapterId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video,videoVo);
                    finalVideoList.add(videoVo);
                }
                chapterVo.setChildren(finalVideoList);
            }
        }
        return finalChapterVos;
    }

    @Override
    public void removeChapterByCourseId(String id) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",id);
        baseMapper.delete(queryWrapper);
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        //根据chapter章节id查询是否有小节，如果有小节，则无法删除
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        int count = (int) eduVideoService.count(queryWrapper);
        //判断
        if(count > 0){
            //存在小节，无法删除
            throw new MyException(20001,"该章节下存在小节，无法删除");
        }else {
            //不存在小节，可删除
            int result = baseMapper.deleteById(chapterId);
            return result > 0;
        }
    }
}
